cmake_minimum_required( VERSION 2.8 )
project( dso_rewrite )

set( BUILD_TYPE Release )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}     -Wall -O3 -march=native")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")

set(EXECUTABLE_OUTPUT_PATH bin)
set(LIBRARY_OUTPUT_PATH lib)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

# other modules 
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)

# required libraries
find_package(SuiteParse REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(Pangolin REQUIRED)
find_package(OpenCV REQUIRED )
find_package(LibZip QUIET)

# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
   add_definitions(-DCOMPILEDWITHC11)
   message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
   add_definitions(-DCOMPILEDWITHC0X)
   message(STATUS "Using flag -std=c++0x.")
else()
   message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

# SSE related 
IF(DEFINED ENV{ARM_ARCHITECTURE})
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon -march=armv7-a")
ELSE()
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmmx -msse -msse -msse2 -msse3 -mssse3 -msse4")
ENDIF()


include_directories(
  ${PROJECT_SOURCE_DIR}/include
  ${PROJECT_SOURCE_DIR}/thirdparty/Sophus
  ${EIGEN3_INCLUDE_DIR}
  ${CSPARSE_INCLUDE_DIR}
  ${CHOLMOD_INCLUDE_DIR}
) 

# decide if we have ziplib.
if (LIBZIP_LIBRARY)
        message("--- found ziplib (${LIBZIP_VERSION}), compiling with zip capability.")
        add_definitions(-DHAS_ZIPLIB=1)
        include_directories( ${LIBZIP_INCLUDE_DIR_ZIP} ${LIBZIP_INCLUDE_DIR_ZIPCONF} ) 
else()
        message("--- not found ziplib (${LIBZIP_LIBRARY}), compiling without zip capability.")
        set(LIBZIP_LIBRARY "")
endif()


# Sources files
set(dso_SOURCE_FILES
  ${PROJECT_SOURCE_DIR}/src/FullSystem/FullSystem.cpp
  ${PROJECT_SOURCE_DIR}/src/FullSystem/Residuals.cpp
  ${PROJECT_SOURCE_DIR}/src/FullSystem/CoarseTracker.cpp
  ${PROJECT_SOURCE_DIR}/src/FullSystem/CoarseInitializer.cpp
  ${PROJECT_SOURCE_DIR}/src/FullSystem/ImmaturePoint.cpp
  ${PROJECT_SOURCE_DIR}/src/FullSystem/HessianBlocks.cpp
  ${PROJECT_SOURCE_DIR}/src/FullSystem/PixelSelector.cpp
  ${PROJECT_SOURCE_DIR}/src/OptimizationBackend/EnergyFunctional.cpp
  ${PROJECT_SOURCE_DIR}/src/OptimizationBackend/AccumulatedTopHessian.cpp
  ${PROJECT_SOURCE_DIR}/src/OptimizationBackend/AccumulatedSCHessian.cpp
  ${PROJECT_SOURCE_DIR}/src/OptimizationBackend/EnergyFunctionStructs.cpp
  ${PROJECT_SOURCE_DIR}/src/util/Settings.cpp
  ${PROJECT_SOURCE_DIR}/src/util/Undistort.cpp
  ${PROJECT_SOURCE_DIR}/src/util/GlobalCalib.cpp
)


include_directories( ${Pangolin_INCLUDE_DIRS} ) 
set(dso_pangolin_SOURCE_FILES 
    ${PROJECT_SOURCE_DIR}/src/IOWrapper/KeyFrameDisplay.cpp
    ${PROJECT_SOURCE_DIR}/src/IOWrapper/PangolinDSOViewer.cpp
)

include_directories( ${OpenCV_INCLUDE_DIRS} )
set(dso_opencv_SOURCE_FILES 
    ${PROJECT_SOURCE_DIR}/src/IOWrapper/ImageDisplay_OpenCV.cpp
    ${PROJECT_SOURCE_DIR}/src/IOWrapper/ImageRW_OpenCV.cpp
)
set(HAS_OPENCV 1)


add_library( dso 
${dso_SOURCE_FILES} 
${dso_opencv_SOURCE_FILES}
${dso_pangolin_SOURCE_FILES}
)

target_link_libraries( dso 
    ${Pangolin_LIBRARIES}
    ${OpenCV_LIBS}
    ${GLOG_LIBRARY}
    cxsparse
)

# build main executable (only if we have both OpenCV and Pangolin)
add_executable(dso_dataset ${PROJECT_SOURCE_DIR}/Examples/main_dso_pangolin.cpp )
target_link_libraries(dso_dataset ${LIBZIP_LIBRARY} ${Pangolin_LIBRARIES} ${OpenCV_LIBS} dso )
